//index.js
let audioCtx = wx.createInnerAudioContext();

Page({
  data: {
    isBgmOpen: true,
    showHome: true,
    showSubject: false,
    showResult: false,
    isSuccess: false,
    showAuth: false,
    userInfo: {}
  },

  onLoad() {
    audioCtx.autoplay = true;
    audioCtx.src = "http://img.tukuppt.com/newpreview_music/09/01/30/5c89d7a026c1f14142.mp3";
    audioCtx.play();

    // 是否授权
    wx.getSetting({
      success: res => {
        console.log(res.authSetting['scope.userInfo']);
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              console.log(res);
              this.setData({
                userInfo: res.userInfo
              })
            }
          })
        } else {
          this.setData({
            showAuth: true
          });
        }
      }
    })
  },

  toSubject() {
    this.setData({
      showHome: false,
      showSubject: true
    });
  },

  toggleBgm() {
    this.setData({
      isBgmOpen: !this.data.isBgmOpen
    });
    this.data.isBgmOpen ? audioCtx.play() : audioCtx.stop();
  },

  renderResult(e) {
    let score = e.detail;
    this.setData({
      showSubject: false,
      showResult: true,
      isSuccess: score >= 0 ? true : false   // 是否通过考核（目前是只要全部答完题目就通过学习）
    });
  },

  // 重新开始
  handleReset() {
    this.setData({
      showHome: true,
      showSubject: false,
      showResult: false,
      isSuccess: false
    });
  },

  // 分享
  onShareAppMessage(res) {
    if (res.from === 'button') {}
    return {
      // title: '全名战“疫”，等你加入',
      title: '我已成为防疫卫士，等你来一起加入～',
      path: '/pages/index/index',
      success: function(res) {}
    }
  },

  // 关闭授权
  closeAuth() {
    this.setData({
      showAuth: false,
      userInfo: {}
    });
  },

  authorized(e) {
    console.log(e.detail.data);
    this.setData({
      showAuth: false,
      userInfo: e.detail.data.userInfo
    })
  }
})